package testJdk.nio.testChannel;

import org.junit.Test;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestFileChannel {

    /**
     * 测试读取数据
     */
    @Test
    public void testRead() throws Exception {
        // 创建channel
        RandomAccessFile accessFile = new RandomAccessFile("D:\\workspace\\demo\\src\\main\\java\\learn_jdk\\test_nio\\test.txt", "rw");
        FileChannel fileChannel = accessFile.getChannel();
        // 创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 从channel中读取
        int read = fileChannel.read(byteBuffer);
        // 从buffer中读取
        while (read != -1) {
            System.out.println("读取了:" + read);
            // 反转buffer的读写模式
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
                System.out.println((char) byteBuffer.get());
            }
            byteBuffer.clear();
            // 再次读取，并改变read的值，如果此时channel中没有数据了，就返回-1
            read = fileChannel.read(byteBuffer);
        }
        accessFile.close();
        System.out.println("操作结束");
    }

    /**
     * 测试写入数据
     */
    @Test
    public void testWrite() throws Exception {
        // 创建channel
        RandomAccessFile accessFile = new RandomAccessFile("D:\\workspace\\demo\\src\\main\\java\\learn_jdk\\test_nio\\test.txt", "rw");
        FileChannel fileChannel = accessFile.getChannel();
        // 创建buffer
        String newStr = "New Strings write to file:" + System.currentTimeMillis() + "--cyd";
        ByteBuffer byteBuffer = ByteBuffer.allocate(48);
        // 向buffer中写入数据
        byteBuffer.put(newStr.getBytes());
        // 反转buffer的读写模式
        byteBuffer.flip();
        // 向fileChannel中写入数据
        while (byteBuffer.hasRemaining()) {
            fileChannel.write(byteBuffer);
        }
        byteBuffer.clear();
        fileChannel.close();
        System.out.println("操作结束");
    }

    /**
     * 测试通道间传输
     */
    @Test
    public void testTransfer() throws Exception {
        RandomAccessFile accessFile = new RandomAccessFile("D:\\WorkSpace\\demo\\src\\main\\java\\learn_jdk\\test_nio\\test.txt", "rw");
        FileChannel fileChannel = accessFile.getChannel();


        RandomAccessFile accessFile2 = new RandomAccessFile("D:\\WorkSpace\\demo\\src\\main\\java\\learn_jdk\\test_nio\\test2.txt", "rw");
        FileChannel fileChannel2 = accessFile2.getChannel();

        long position = 0L;
        long count = fileChannel.size();

        fileChannel2.transferFrom(fileChannel, position, count);

        fileChannel.close();
        fileChannel2.close();

        System.out.println("over");

    }

    @Test
    public void testTransFormTo() throws Exception {
        RandomAccessFile aFile = new RandomAccessFile("d:\\atguigu\\02.txt", "rw");
        FileChannel fileChannel1 = aFile.getChannel();
        RandomAccessFile bFile = new RandomAccessFile("d:\\atguigu\\03.txt", "rw");
        FileChannel fileChannel2 = bFile.getChannel();
        long position = 0;
        long count = fileChannel1.size();
        fileChannel1.transferTo(position, count, fileChannel2);
        aFile.close();
        bFile.close();
        System.out.println("over!");
    }
}


